﻿using MetaView.Shell.Views;
using Microsoft.Extensions.DependencyInjection;
using System.IO;
using Wpf.Ui;

namespace MetaView.Shell;

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : WpfApplication
{
    protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
    {
        //可以直接注册模块
        //moduleCatalog.Add<SayHelloModule>;
    }

    /// <summary>
    /// 创建主窗口
    /// </summary>
    /// <param name="windowManager"></param>
    /// <returns></returns>
    protected override Window CreateMainWindow(IWindowManager windowManager)
    {
        return windowManager.GetWindow<Views.Windows.MainWindowView>();
    }

    protected override bool FindModule(string path)
    {
        var fileName = Path.GetFileNameWithoutExtension(path);
        //return fileName.EndsWith("Module");
        return fileName != null &&
           fileName.StartsWith("MetaView.Module.", StringComparison.OrdinalIgnoreCase) &&
           fileName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase);
    }

    protected override Task InitializeAsync(AppModuleInitEventArgs e)
    {
        //程序初始化时执行
        //可以在这里注册服务

        //注册单例模式的View和ViewModel
        // e.Services.AddSingletonView<MainWindow, MainWindowViewModel>();
        e.Services.AddSingletonView<Views.Windows.MainWindowView, ViewModels.Windows.MainWindowViewModel>();

        e.Services.AddSingleton<INavigationService, NavigationService>();
        e.Services.AddSingleton<ISnackbarService, SnackbarService>();
        e.Services.AddSingleton<IContentDialogService, ContentDialogService>();

        e.Services.AddSingletonView<Views.Pages.DashboardPage, ViewModels.Pages.DashboardViewModel>();
        e.Services.AddSingletonView<Views.Pages.DataPage, ViewModels.Pages.DataPageViewModel>();
        e.Services.AddSingletonView<Views.Pages.SettingsPage, ViewModels.Pages.SettingsViewModel>();

        //e.Services.AddSingletonNavigate<RegionControl, RegionControlViewModel>("RegionControl");
        return Task.CompletedTask;
    }

    protected override void OnException(Exception ex)
    {
        base.OnException(ex);

        MessageBox.Show($"异常：{ex.Message}");
    }

    protected override Task StartupAsync(AppModuleStartupEventArgs e)
    {
        //程序启动时执行
        //可以在这里通过ServiceProvider获取服务
        //this.ServiceProvider.Resolve<MainViewModel>();
        return Task.CompletedTask;
    }
}
